package br.ufmg.dees.insane.parser;

import java.util.Observable;
import java.util.Observer;

import br.ufmg.dees.insane.model.femModel.FemModel;
import br.ufmg.dees.insane.model.postp.StepPostp;
import br.ufmg.dees.insane.solution.EquilibriumPath;
import br.ufmg.dees.insane.solution.Solution;
import br.ufmg.dees.insane.solution.SteadyState;

/**
 * This class fill a PostpModel from a FemModel.
 * @author Samuel Silva Penna
 * @since 16/06/2006
 */
public class FemModelToPostpModel extends Observable implements Observer {

	private FemModel fem;
	private Solution solution;
	private StepPostp step;
	private int stepLabel;
	//private ArrayList<StepPostp> steps = new ArrayList<StepPostp>();
	private double FinalLoadFactor = 0.0;
	
	public FemModelToPostpModel(FemModel fem){
		this.fem = fem;
		this.step = new StepPostp();
	}
	
	public FemModelToPostpModel(FemModel fem,Solution solution){
		this.fem = fem;
		this.solution = solution;
		this.step = new StepPostp();
	}
		
	public StepPostp getStepPostp(){
		NodalFemToGeoPostpModel parserN = new NodalFemToGeoPostpModel(fem);
		step.addGeoPostpModel(parserN.getGeoPostpModel());
		GaussFemlToGeoPostpModel parserG = new GaussFemlToGeoPostpModel(fem);
		step.addGeoPostpModel(parserG.getGeoPostpModel());
		setStepValues();
		return step;
	}
	
	public void setStepValues(){
		if(this.solution instanceof EquilibriumPath){
			EquilibriumPath s = (EquilibriumPath)this.solution;
			this.step.setLabel(""+stepLabel);
			this.step.putStepValue("NumMaxIterarions",new Double(s.getchangeNumMaxIterations()));
			this.step.putStepValue("NumMaxSteps",new Double(s.getNumMaxSteps()));
			this.step.putStepValue("ChangeLoadFactor",new Double(s.getchangeLoadFactor()));
			this.step.putStepValue("FinalLoadFactor",new Double(s.getStep().getLoadFactorTotal()));
		}else if(this.solution instanceof SteadyState){
			//SteadyState s = (SteadyState)this.solution;
			this.step.setLabel(""+stepLabel);
		}
		else{
            this.step.putStepValue("FinalLoadFactor",new Double(FinalLoadFactor));
            this.step.setLabel(""+stepLabel);
        }
		
	}
	
	public Solution getSolution() {
		return solution;
	}

	public void setSolution(Solution solution) {
		this.solution = solution;
	}

	public double getFinalLoadFactor() {
		return FinalLoadFactor;
	}

	public void setFinalLoadFactor(double finalLoadFactor) {
		FinalLoadFactor = finalLoadFactor;
	}
	
	//This class observe the femModel and is Observerd by parserHalfEdge 
	public void update(Observable o, Object arg) {
		fem = (FemModel)o; 
		step = new StepPostp();
		getStepPostp();
		setChanged();
	    hasChanged();
	    notifyObservers();
	}
    
    /* (non-Javadoc)
     * @see java.util.Observable#addObserver(java.util.Observer)
     */
    public synchronized void addObserver(Observer arg0) {
        super.addObserver(arg0);
    }
    
    /* (non-Javadoc)
     * @see java.util.Observable#deleteObserver(java.util.Observer)
     */
    public synchronized void deleteObserver(Observer arg0) {
        super.deleteObserver(arg0);
    }
    
    /* (non-Javadoc)
     * @see java.util.Observable#deleteObservers()
     */
    public synchronized void deleteObservers() {
        super.deleteObservers();
    }
    
    /* (non-Javadoc)
     * @see java.util.Observable#hasChanged()
     */
    public synchronized boolean hasChanged() {
        return super.hasChanged();
    }
    
    /* (non-Javadoc)
     * @see java.util.Observable#notifyObservers()
     */
    public void notifyObservers() {
        super.notifyObservers();
    }

	public int getStepLabel() {
		return stepLabel;
	}

	public void setStepLabel(int stepLabel) {
		this.stepLabel = stepLabel;
	}
    
//***************************************************************************
}//end of this class


